package com.situ.storehouse.dao;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.situ.storehouse.entity.Attendant;
import com.situ.storehouse.util.Dbutil;

public class AttendantDao {

	public void add(Attendant attendant) {
		String sql = "insert into attendant(username,password,birthday,sex,cash) values(?,?,?,?,?)";
		Dbutil.executeDML(sql,attendant.getUsername(),attendant.getPassword(),attendant.getBirthday(),attendant.getSex()
				,attendant.getCash());
	}

	public void delete(Integer id) {
		String sql = "delete from attendant where id=?";
		Dbutil.executeDML(sql, id);
	}

	public void update(Attendant attendant) {
		String sql = "update attendant set username=?,password=?,birthday=?,sex=?,cash=? where id=?";
		Dbutil.executeDML(sql,attendant.getUsername(),attendant.getPassword(),attendant.getBirthday(),attendant.getSex()
				,attendant.getCash(),attendant.getId());
	}

	public Attendant select(Integer id) {
		String sql = "select * from attendant where id=?";
		List<Attendant> list = Dbutil.executeDQL(sql,Attendant.class, id);
		return list.get(0);
	}
	
	public List<Attendant> select(Integer page,Integer limit,String keyword) {
		String sql=null;
		if(keyword==null || keyword.equals("")) {
		sql = "select * from attendant limit ?,?";
		}else{
			sql="select * from attendant where instr(username,'"+keyword+"')>0 limit ?,?";
		}
		return Dbutil.executeDQL(sql, Attendant.class,page,limit);
		
	}

	public Long count(String keyword) {
		String sql=null;
		if(keyword==null || keyword.equals("")) {
			sql="select count(*) from attendant";
		}else {
			sql="select count(*) from attendant  where instr(username,'"+keyword+"')>0 ";
		}
		
		return Dbutil.executeCount(sql);
	}
		
	public Boolean check(String username,String password,HttpSession session) {
		String sql="select * from attendant where username=?";
		List<Attendant> list=Dbutil.executeDQL(sql, Attendant.class, username);
		Boolean b= list.size()>0&& list.get(0).getPassword().equals(password);
		if(b) {//如果进入说明已经登录成功
			session.setAttribute("currentAttendant", list.get(0));
		}
		return b;
	}

	public List<Attendant> selectByUsername(String username) {
		String sql="select * from attendant where username=?";
		return Dbutil.executeDQL(sql, Attendant.class, username);
	}

	
}
